package com.ruoyi.common.service;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiProcessinstanceGetRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiProcessinstanceGetResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.ruoyi.common.utils.dd.AccessTokenUtil;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author YanWenliang
 * @date 2024/8/9 16:42
 * @desciption:
 */
@Component
@Slf4j
public class ProcessService {

    /**
     * 审批通过
     * @param processCode
     * @param processInstanceId
     * @return
     */
    @Transactional
    public boolean agree(String processCode,String processInstanceId){

        return true;
    }

    /**
     * 审批未通过
     * @param processCode
     * @param processInstanceId
     * @return
     */
    @Transactional
    public boolean refuse(String processCode,String processInstanceId){

        return true;
    }

    /**
     *  通过审批实例ID获取审批详情
     * @param instanceId
     * @return
     */
    public OapiProcessinstanceGetResponse.ProcessInstanceTopVo getProcessinstanceById(String instanceId){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
        OapiProcessinstanceGetRequest request = new OapiProcessinstanceGetRequest();
        request.setProcessInstanceId(instanceId);
        OapiProcessinstanceGetResponse.ProcessInstanceTopVo processInstance = null;
        try {
            OapiProcessinstanceGetResponse execute = client.execute(request, AccessTokenUtil.getAccessToken());
            processInstance = execute.getProcessInstance();
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return processInstance;
    }

    /**
     * 获取用户详情
     * @param userId
     */
    public String getUserName(String userId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
        req.setUserid(userId);
        req.setLanguage("zh_CN");
        OapiV2UserGetResponse rsp = client.execute(req, AccessTokenUtil.getAccessToken());
        return rsp.getResult().getName();
    }

    /**
     * 获取
     * @param userId
     */
    public OapiV2UserGetResponse.UserGetResponse getUserInfo(String userId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
        req.setUserid(userId);
        req.setLanguage("zh_CN");
        OapiV2UserGetResponse rsp = client.execute(req, AccessTokenUtil.getAccessToken());
        return rsp.getResult();
    }

}
